package com.smart.campus.web.controller.school;

import com.smart.campus.common.annotation.Log;
import com.smart.campus.common.core.controller.BaseController;
import com.smart.campus.common.core.domain.AjaxResult;
import com.smart.campus.common.core.page.TableDataInfo;
import com.smart.campus.common.enums.BusinessType;
import com.smart.campus.framework.util.ShiroUtils;
import com.smart.campus.school.domain.ApplyList;
import com.smart.campus.school.domain.ApproveList;
import com.smart.campus.school.domain.Student;
import com.smart.campus.school.domain.TeacherApplyList;
import com.smart.campus.school.service.IApproveListService;
import com.smart.campus.school.service.IPersonnelInfoService;
import com.smart.campus.school.service.ITeacherApplyListService;
import com.smart.campus.system.domain.SysUser;
import com.smart.campus.system.service.ISysUserService;
import com.smart.campus.web.utils.SchoolUtil;
import com.smart.campus.web.utils.StudentUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 老师请假申请Controller
 * 
 * @author Zhihe.Li
 * @date 2020-06-15
 */
@Controller
@RequestMapping("/school/teacher/apply")
public class TeacherApplyListController extends BaseController
{
    @Autowired
    private ISysUserService userService;
    private String prefix = "school/apply/teacher";

    @Autowired
    private ITeacherApplyListService applyListService;
    @Autowired
    private IApproveListService approveListService;

    @Autowired
    private IPersonnelInfoService personnelInfoService;

    @GetMapping()
    public String apply()
    {
        return prefix + "/apply";
    }

    /**
     * 查询老师请假申请列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TeacherApplyList applyList)
    {
        startPage();
        applyList.setSchoolId(SchoolUtil.getSchoolId());
        applyList.setTeacherId(ShiroUtils.getUserId());
        List<TeacherApplyList> list = applyListService.selectTeacherApplyListList(applyList);
        return getDataTable(list);
    }
    /**
     * 查询老师请假申请列表
     */
    @PostMapping("/waitApproveList")
    @ResponseBody
    public TableDataInfo waitApproveList(TeacherApplyList applyList)
    {
        startPage();
        applyList.setSchoolId(SchoolUtil.getSchoolId());
        applyList.setApproverIds(ShiroUtils.getUserId().toString());
        List<TeacherApplyList> list = applyListService.selectTeacherApplyListList(applyList);
        return getDataTable(list);
    }
    @GetMapping("/waitApprove")
    public String waitApprove()
    {
        return prefix + "/waitApprove";
    }
    /**
     * 新增老师请假申请
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
        SysUser user = ShiroUtils.getSysUser();
        modelMap.put("teacher",user);
        return prefix + "/add";
    }

    /**
     * 新增保存老师请假申请
     */
    @Log(title = "老师请假申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TeacherApplyList applyList)
    {

        applyList.setSchoolId(SchoolUtil.getSchoolId());
        return toAjax(applyListService.insertTeacherApplyList(applyList));
    }

    /**
     * 修改老师请假申请
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TeacherApplyList applyList = applyListService.selectTeacherApplyListById(id);
        mmap.put("applyList", applyList);
        return prefix + "/edit";
    }

    /**
     * 修改保存老师请假申请
     */
    @Log(title = "老师请假申请", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TeacherApplyList applyList)
    {
        return toAjax(applyListService.updateTeacherApplyList(applyList));
    }


    /**
     * 修改老师请假申请
     */
    @GetMapping("/getApproveList/{id}")
    public String getApproveList(@PathVariable("id") Long id, ModelMap mmap)
    {
        TeacherApplyList applyList = applyListService.selectTeacherApplyListById(id);

        mmap.put("applyList", applyList);
        return prefix + "/approveList";
    }

    /**
     * 审批页面
     * @return
     */
    @GetMapping("/approve/{id}")
    public String approve(@PathVariable("id") Long id, ModelMap mmap)
    {
        TeacherApplyList applyList = applyListService.selectTeacherApplyListById(id);

        mmap.put("applyList", applyList);
        return prefix + "/approve";
    }

    /**
     * 审批
     */
    @PostMapping("/approve")
    @ResponseBody
    public AjaxResult approve(ApproveList approveList)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        approveList.setApproveId(sysUser.getUserId());
        approveList.setApprover(sysUser.getUserName());
        TeacherApplyList old = applyListService.selectTeacherApplyListById(approveList.getApplyId());
        int status = SchoolUtil.getApplySpStatus(approveList.getStatus(),old.getStatus());
        TeacherApplyList applyList = new TeacherApplyList();
        applyList.setStatus(status);
        applyList.setId(approveList.getApplyId());
        applyListService.updateTeacherApplyList(applyList);

        approveList.setType(2);
        approveListService.insertApproveList(approveList);
        return AjaxResult.success();
    }

}
